package com.woniuxy.joylifecenter.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.CenterLog;
import com.woniuxy.joylifecenter.annotation.SysLog;
import com.woniuxy.joylifecenter.service.LogService;
import com.woniuxy.joylifecenter.vo.VO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Slf4j
@Controller
@Api(value = "",description = "后台controller")
public class Joylife_Center_Controller {
    @Autowired
    LogService logService;

    @ApiOperation(value = "/index",notes = "访问主页")
    @SysLog(value = "访问主页")
    @RequestMapping("/index")
    public String indexPage() {
        return "index";
    }


    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/2
     * @Param []
     * @Description 带分页查询所有日志
     **/
    @ResponseBody
    @SysLog(value = "查询所有日志")
    @RequestMapping("/showAllLog")
    public VO showAllLog(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(logService.queryAllLog());
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        System.out.println("pageInfos是：" + pageInfos);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        //此处直接从Service返回的集合长度获取总记录条数
        vo.setCount(logService.queryAllLog().size());
        vo.setData(pageInfos);
        return vo;
    }

    /**
     * @return com.woniuxy.joylifecenter.vo.VO
     * @Author ArmWatt
     * @Date 2020/5/3
     * @Param [page, limit, id, op, ip, name, date]
     * @Description 带分页多条件模糊查询日志
     **/
    @ResponseBody
    @SysLog(value = "多条件模糊查询日志")
    @RequestMapping("/showLogByCondition")
    public VO showLogByCondition(@RequestParam("page") Integer page,
                                 @RequestParam("limit") Integer limit,
                                 @RequestParam("id") String id,
                                 @RequestParam("op") String op,
                                 @RequestParam("ip") String ip,
                                 @RequestParam("name") String name,
                                 @RequestParam("date") Date date) {
        CenterLog centerLog = new CenterLog();
        centerLog.setCenterIp(ip);
        centerLog.setCenterCreatetime(date);
        centerLog.setCenterUsername(name);
        centerLog.setCenterOperation(op);
        centerLog.setCenterLogid(id);
        PageHelper.startPage(page, limit);
        List<CenterLog> centerLogs = logService.queryLogByCondition(centerLog);
        PageInfo pageInfo = new PageInfo(centerLogs);
        List<PageInfo> pageInfos = new ArrayList<PageInfo>();
        pageInfos.add(pageInfo);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("");
        vo.setData(pageInfos);
        //此处需要从pageInfo获取总记录条数，不能直接用Service返回的集合大小
        vo.setCount(new Long(pageInfo.getTotal()).intValue());
        return vo;
    }


    @ResponseBody
    @SysLog(value = "手动批量删除日志")
    @RequestMapping("/batchDeleteLogManually")
    public VO batchDeleteLogManually(@RequestBody List<String> centLogIds) {

        System.out.println("controller接收到的字符串数组是：" + centLogIds);
        int count = logService.batchDeleteLogManually(centLogIds);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("批量删除了：");
        vo.setData(null);
        vo.setCount(count);
        return vo;
    }

    @ResponseBody
    @SysLog(value = "手动删除单条日志")
    @RequestMapping("/deleteLogManually")
    public VO deleteLogManually(@RequestBody String logId) {
        int count = logService.deleteLogManually(logId);
        System.out.println(logId);
        VO vo = new VO();
        vo.setCode(0);
        vo.setMsg("本次删除了：");
        vo.setData(null);
        vo.setCount(count);
        return vo;
    }


}
